import 'package:flutter/cupertino.dart'
    show CupertinoTextField, OverlayVisibilityMode;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../../common/utils/screen_util.dart';
import '../../config.dart';
import '../resources/colors.dart';

typedef CupOverlayVisibilityMode = OverlayVisibilityMode;

class MyTextField extends StatefulWidget {
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final String? initText;
  final String? hintText;
  final String? labelText;

  final int? maxLength;
  final int? maxLines;
  final TextAlign? textAlign;
  final TextInputType? keyboardType; //键盘类型
  final Widget? prefix;
  final Widget? suffix;
  final OverlayVisibilityMode suffixMode;
  final OverlayVisibilityMode? clearButtonMode; //清空按钮

  final bool autofocus;

  //fontSize
  final double fontSize;

  //fontColor
  final Color? fontColor;
  final Color? hintColor;

  //背景颜色
  final Color? backgroundColor;

  //border
  final bool border;

  final ValueChanged<String>? onChanged;
  final ValueChanged<String>? onSubmitted;

  final bool obscureText;

  const MyTextField({
    Key? key,
    this.controller,
    this.focusNode,
    this.hintText,
    this.labelText,
    this.maxLength,
    this.textAlign,
    this.keyboardType,
    this.onChanged,
    this.onSubmitted,
    this.prefix,
    this.autofocus = false,
    this.backgroundColor,
    this.fontSize = 17,
    this.fontColor,
    this.hintColor,
    this.border = true,
    this.suffix,
    this.suffixMode = OverlayVisibilityMode.always,
    this.clearButtonMode,
    this.initText,
    this.obscureText = false,
    this.maxLines = 1,
  }) : super(key: key);

  @override
  State<MyTextField> createState() => _MyTextFieldState();
}

class _MyTextFieldState extends State<MyTextField> {
  @override
  Widget build(BuildContext context) {
    return CupertinoTextField(
      controller: widget.controller ??
          (widget.initText != null && widget.initText!.isNotEmpty
              ? TextEditingController(text: widget.initText)
              : null),
      focusNode: widget.focusNode,
      placeholder: widget.hintText,
      placeholderStyle: TextStyle(
          color: widget.hintColor ?? Colors.grey,
          fontSize: widget.fontSize,
          height: 1.4,
          fontFamily: JmConfig.defTextStyle.fontFamily),
      style: TextStyle(
        color: widget.fontColor ?? Colors.black,
        fontSize: widget.fontSize,
        fontFamily: JmConfig.defTextStyle.fontFamily,
        height: 1.4,
      ),
      obscureText: widget.obscureText,
      prefix: widget.prefix ??
          (widget.labelText == null
              ? null
              : Padding(
                  padding: const EdgeInsets.only(left: 8),
                  child: Text(widget.labelText!))),
      suffix: widget.suffix,
      suffixMode: widget.suffixMode,
      clearButtonMode: widget.clearButtonMode == null
          ? kIsWeb
              ? OverlayVisibilityMode.always
              : OverlayVisibilityMode.editing
          : widget.clearButtonMode!,
      autofocus: widget.autofocus,
      keyboardType: widget.keyboardType,
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        //color: const Color(0xFFeeeef0),
        color: widget.backgroundColor ?? Colors.white,
        borderRadius: BorderRadius.circular(8),
        border: widget.border
            ? Border.all(color: JmColors.div, width: MyScreen.onePixel)
            : null,
      ),
      onChanged: widget.onChanged,
      onSubmitted: widget.onSubmitted,
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
    );
  }
}
